import { loginAPI } from '@/api/user'
import { TOKEN_KEY } from '@/constants/KEY'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { getProfileAPI } from '@/api/user'
export default {
  namespaced: true,
  state: () => {
    return {
      token: getToken() || '',
      profile: {}
    }
  },
  mutations: {
    setToken(state, token) {
      state.token = token
      // 本地存一份
      state.profile = {}
      setToken(token)
    },
    clearUserInfo(state) {
      state.token = ''
      removeToken(TOKEN_KEY)
    },
    setProfile(state, profile) {
      state.profile = profile
    }
  },
  actions: {
    async login(context, data) {
      const res = await loginAPI(data)
      console.log(res)
      context.commit('setToken', res.data.token)
    },
    async getProfile(context) {
      const res = await getProfileAPI()
      context.commit('setProfile', res.data)
      return res.data.permissions
    }
  }
}
